package esempi.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This example shows how to use transforms into a Graphics. 
 * 
 * @author Alessandro Martinelli
 */
public class Esempio05Transforms {

	public static void main(String[] args) {
		
		JFrame frame1=new JFrame();
		frame1.setSize(270,270);
		frame1.setTitle("java.awt.Graphics");
		frame1.getContentPane().add(new ExamplePanel());
		frame1.setVisible(true);
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public static class ExamplePanel extends JPanel{
		
		private BufferedImage image1;

		public ExamplePanel() {
			try {
				image1=ImageIO.read(new File("images/text.png"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		protected void paintComponent(Graphics graphics) {
			super.paintComponent(graphics);
			Graphics2D graphics2=(Graphics2D)graphics;
			graphics2.setBackground(Color.WHITE);

			//Rotazione di Math.PI e posizionamento in 30 30
			drawTransformedImage(graphics2 , new AffineTransform(1,0,0,1,30,30));
			
			//Rotazione rispetto all'origine di 30° e posizionamento in 130 30
			double alpha=Math.PI/6;
			double cos=(float)Math.cos(alpha);
			double sin=(float)Math.sin(alpha);
			drawTransformedImage(graphics2, new AffineTransform(cos,-sin,sin,cos, 130, 30 ));
		
			//Scaling e posizionamento in 30 130
			double scale=0.4f;
			drawTransformedImage(graphics2, new AffineTransform(scale,0,0,scale, 30, 130 ));

			//'Shearing' sull'asse x e posizionamento in 130 130
			double shear_x=-0.2f;
			double shear_y=0.0f;
			drawTransformedImage(graphics2, new AffineTransform(1,shear_y,shear_x,1, 130, 130 ));
				
			//'Shearing' sull'asse y e posizionamento in 230 30
			double shear_x_2=0.2f;
			double shear_y_2=0.2f;
			drawTransformedImage(graphics2, new AffineTransform(1,shear_y_2,shear_x_2,1, 230, 30 ));
		
		}

		private void drawTransformedImage(Graphics2D graphics2,AffineTransform tr) {
			graphics2.setTransform(tr);
			graphics2.drawImage(image1,0,0, null);
		}
	}
	
	
	
}
